<template>
  <div class="full-height bg-white page-content content">
    <collapse ref="collapse" class="collapse-content after">
      <template v-slot:content>
        <yz-condition-filter
          v-model="queryParams"
          @resetSearch="resetSearch"
          @onSubmit="onSubmit"
        >
        <select-tooltip
            v-model="queryParams.workshopId"
            :multiple="false"
            isShowAll
            @input="onSelectWorkShop"
            :options="workshopslist"
            msg="所在车间"
          ></select-tooltip>
        <select-tooltip
            v-model="queryParams.deviceType"
            :multiple="false"
            isShowAll
            :options="deviceTypesList"
            msg="设备类型"
          ></select-tooltip>
            <fuzzy-search
              v-model.trim="queryParams.deviceCode"
              :fuzzyParams="fuzzyParams"
              fuzzyValue="assetNumber"
              :fuzzyObj="['name', 'id']"
              isFilter
              class="margin-r-20"
              isShowAll
              title="加工设备"
              :fuzzyFunc="fuzzyFunc"
            ></fuzzy-search>
        <yz-input
              v-model.trim="queryParams.dateRange"
              class="q-mr-md"
              inputType="date"
              hint="请选择日期范围"
              isTip
              title="保养日期"
         ></yz-input>
        <select-tooltip
            v-model="queryParams.maintenanceType"
            :multiple="false"
            isShowAll
            :options="maintenanceTypeOptions"
            msg="保养类型"
          ></select-tooltip>
          <select-tooltip
            v-model="queryParams.programId"
            :multiple="false"
            isShowAll
            :options="programList"
            msg="保养项目"
          ></select-tooltip>
        </yz-condition-filter>
        <div class="form-inline bottom justify-between padding-b-20">
          <div class="left box">
              <export-btn
                  :disable="!tableData.length"
                  :exportFun="exportFun"
                  :exportParams="queryParams"
                  :exportName="getExportName()"/>
          </div>
          <div>
              <select-input ref="selectInput"
                            :list="searchCondition"
                            @search="inputSearch"
                            @clearSearchVal="clearSearchVal" />
          </div>
        </div>
      </template>
    </collapse>
    <q-table
            :data="tableData"
            :columns="columns"
            row-key="id"
            class="myTable scroll stick-last-col"
            :loading="isTableLoading"
            :pagination.sync="pagination"
            :hide-bottom="tableData.length > 0"
            color="primary"
          >
      <template v-slot:header-cell-startTime="props">
        <q-th :props="props"
              class="sortable sorted-icon"
              @click="onSeletByTime">
            {{props.col.label}}
            <q-icon class="q-table__sort-icon"  :name="queryParams.sort ? 'arrow_upward' : 'iconfont iconjiantoupaixu'"></q-icon>
        </q-th>
      </template>
      <template v-slot:body-cell-maintenanceTypeName="props">
        <q-td :props="props">
          {{props.row.maintenanceTypeName | toHorizontalLine}}
        </q-td>
      </template>
      <template v-slot:body-cell-operations="props">
        <q-td class="operations" style="width: 56px">
          <div class="btn-group">
            <table-btn btnType="detail" @click="onDetail(props.row)"/>
          </div>
        </q-td>
      </template>
    </q-table>
    <yz-pagination v-model="pagination" @onSearch="getList"/>
    </div>
</template>
<script>
import Collapse from 'src/components/collapse/index'
import MAINTENANCEMIXIN from './maintenance-mixin'
import SelectInput from 'src/components/select-input/index'
import {
  getMaintenaceRecordsList,
  getExportRecord,
  getMaintenancePrograms
} from 'src/boot/maintenance/maintenance-order'
// import _ from 'lodash'
// import moment from 'moment'
export default {
  name: 'maintenance-record',
  mixins: [MAINTENANCEMIXIN], // 相关变量, 函数,在这个文件, 和保养工单, 相差不多
  components: {
    Collapse,
    SelectInput
  },
  data () {
    return {
      exportFun: getExportRecord,
      queryParams: {
        dateRange: [],
        startDate: '',
        endDate: '',
        workshopId: 'all',
        maintenanceType: 'all',
        programId: 'all',
        deviceType: 'all',
        deviceCode: '',
        sort: 0
      },
      keyWord: '',
      keyType: '', // 1-保养单号 2-保养人
      tableData: [], // 列表数据源
      isTableLoading: false, // table数据列表loading动画
      columns: [
        { name: 'orderNo', label: '保养单号', align: 'left', field: 'orderNo' },
        { name: 'assetNumber', label: '设备编号', align: 'left', field: 'assetNumber' },
        { name: 'deviceName', label: '设备名称', align: 'left', field: 'deviceName' },
        { name: 'deviceTypeName', label: '设备类型', align: 'left', field: 'deviceTypeName' },
        { name: 'maintenanceTypeName', label: '保养类型', align: 'left', field: 'maintenanceTypeName' },
        { name: 'maintenanceProgramName', label: '保养项目', align: 'left', field: 'maintenanceProgramName' },
        { name: 'startTime', label: '保养开始时间', align: 'left', field: 'startTime' },
        { name: 'endTime', label: '保养结束时间', align: 'left', field: 'endTime' },
        { name: 'maintenanceTime', label: '保养时长', align: 'left', field: 'maintenanceTime' },
        { name: 'operatorNameList', label: '保养人', align: 'left', field: 'operatorNameList' },
        { name: 'remake', label: '备注', align: 'left', field: 'remake' },
        {
          name: 'operations',
          field: 'operations',
          label: '操作',
          align: 'center'
        }
      ],
      programList: [],
      searchCondition: [
        {
          id: '1',
          name: '保养单号'
        },
        {
          id: '2',
          name: '保养人'
        }
      ]
    }
  },
  computed: {
    //  保养类型枚举
    maintenanceTypeOptions () {
      return this.$getEnum(this.Enums.MAINTENANCE_TYPE)
    }
  },
  beforeRouteEnter (to, from, next) {
    to.name === 'maintenanceRecord' && next(vm => {
      vm.getPrograms()
      vm.getList()
    })
  },
  methods: {
    onDetail (row) {
      this.$router.push({
        path: '/main/repair/maintenance-record/detail-record',
        query: {
          id: row.id
        }
      })
    },
    async getPrograms () {
      const res = await getMaintenancePrograms()
      this.programList = res.data.list.map(v => {
        return {
          ...v,
          label: v.name,
          value: v.id
        }
      })
    },
    // 获取导出报表名称
    getExportName () {
      let name = '保养记录表.xls'
      return name
    },
    // 模糊查询
    inputSearch (item, searchVal) {
      console.log(searchVal)
      this.pagination.pageNum = 1
      this.keyWord = searchVal
      this.keyType = item.id
      this.getList()
    },
    // 清空搜索值
    clearSearchVal () {
      this.keyWord = ''
      this.keyType = ''
      this.pagination.pageNum = 1
      this.getList()
    },
    getList () {
      let [ startDate, endDate ] = this.$utils.setDateOSI(this.queryParams.dateRange, true, 'YYYY-MM-DD')
      this.queryParams.startDate = startDate
      this.queryParams.endDate = endDate
      const params = {
        ...this.queryParams,
        ...this.pagination,
        keyWord: this.keyWord,
        keyType: this.keyType
      }
      this.isTableLoading = true
      getMaintenaceRecordsList(params).then((res) => {
        this.isTableLoading = false
        const ret = res.data
        this.tableData = ret.list.map(v => {
          const item = v
          item.maintenanceTime = v.maintenanceTime ? this.$utils.secondToString(v.maintenanceTime) : '--'
          item.operatorNameList = v.operatorNameList ? v.operatorNameList.join() : '--'
          return item
        })
        this.pagination.rowsNumber = ret.total
      }).catch((err) => {
        this.isTableLoading = false
        console.log(err)
      })
    }
  }
}
</script>
